package cn.topevery.garden.dal.util;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 时间操作
 **/
public class DurationUtil {

    /**
     * 分钟差
     **/
    public static long subtractMinutes(LocalDateTime startInclusive, LocalDateTime endExclusive) {
        Duration duration = Duration.between(endExclusive, startInclusive);
        return duration.toMinutes();
    }

    /**
     * 小时差
     **/
    public static long subtractHours(LocalDateTime startInclusive, LocalDateTime endExclusive) {
        Duration duration = Duration.between(endExclusive, startInclusive);
        return duration.toHours();
    }

    /**
     * 毫秒差
     **/
    public static long subtractMillis(LocalDateTime startInclusive, LocalDateTime endExclusive) {
        Duration duration = Duration.between(endExclusive, startInclusive);
        return duration.toMillis();
    }

    /**
     * 秒差
     **/
    public static long subtractSeconds(LocalDateTime startInclusive, LocalDateTime endExclusive) {
        long millis = subtractMillis(startInclusive, endExclusive);
        long seconds = millis / 1000;
        return seconds;
    }

    /**
     * 是否为同年同月同日
     **/
    public static boolean isSameDay(LocalDate begin, LocalDate end) {
        return begin.equals(end);
    }

    /**
     * 是否为同年同月同日
     **/
    public static boolean isSameDay(LocalDateTime begin, LocalDateTime end) {
        return isSameDay(begin.toLocalDate(), end.toLocalDate());
    }

    /**
     * begin是否大于end
     **/
    public static boolean isAfter(LocalDateTime begin, LocalDateTime end) {
        return begin.isAfter(end);
    }

    /**
     * begin是否小于end
     **/
    public static boolean isBefore(LocalDateTime begin, LocalDateTime end) {
        return begin.isBefore(end);
    }

    /**
     * begin是否大于等于end
     **/
    public static boolean ge(LocalDateTime begin, LocalDateTime end) {
        return begin.isAfter(end) || begin.equals(end);
    }

    /**
     * begin是否小于等于end
     **/
    public static boolean le(LocalDateTime begin, LocalDateTime end) {
        return begin.isBefore(end) || begin.equals(end);
    }

    /**
     * dateTime>= begin 且 dateTime<=end
     **/
    public static boolean between(LocalDateTime dateTime, LocalDateTime begin, LocalDateTime end) {
        return ge(dateTime, begin) && le(dateTime, end);
    }

    public static String toString(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String toString(LocalDateTime localDateTime) {
        return toString(localDateTime, "yyyy-MM-dd HH:mm:ss");
    }

    public static String toString(LocalDate localDate, String pattern) {
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String toString(LocalDate localDate) {
        return toString(localDate, "yyyy-MM-dd");
    }

    public static String toString(LocalTime localTime, String pattern) {
        return localTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String toString(LocalTime localTime) {
        return toString(localTime, "HH:mm:ss");
    }
}
